import pandas as pd
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.preprocessing import LabelEncoder, StandardScaler

train_file_path = 'data/train.csv'

data_train = pd.read_csv(train_file_path)

# Remove unused columns
data_train = data_train.drop(['Name', 'Ticket', 'Cabin'], axis=1)

label_encoder_sex = LabelEncoder()
data_train['Sex'] = label_encoder_sex.fit_transform(data_train['Sex'])

# Rearrange columns
data_train = data_train[['PassengerId', 'Sex', 'SibSp', 'Parch', 'Pclass', 'Survived']]

# Split column as input and output data
X_train = data_train.iloc[:, 0:5]  # Inputs
y_train = data_train.iloc[:, 5]  # Output(Survived)

sc = StandardScaler()
X_train = sc.fit_transform(X_train)

# Define the classifier
classifier = Sequential()
# Input layer with 5 inputs neurons
classifier.add(Dense(input_dim=5, units=3, kernel_initializer='uniform', activation='relu'))
classifier.add(Dense(units=2, kernel_initializer='uniform', activation='relu'))
classifier.add(Dense(units=1, kernel_initializer='uniform', activation='sigmoid'))

# Compile the model
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Train the model
classifier.fit(X_train, y_train, batch_size=10, epochs=100)

# Save the model
titanic_survival_model_filepath = 'models/titanic_survival_model.h5'
classifier.save(titanic_survival_model_filepath)
